package com.studymaster.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.studymaster.dao.EventDao;
import com.studymaster.domain.Event;

@Repository
public class EventDaoImpl implements EventDao{

	private static final String SEARCH_SQL = "select %s from %s %s";
	private static final String INSERT_SQL = "INSERT INTO event(studentid, timeline, theme, priority, day" +
			", role, site, name, startdate, enddate, memo) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
	private static final String DELETE_SQL = "delete from event where %s";
	
	private static final String TABLE_EVENT = "event";

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	protected RowMapper mReportRowMap = new RowMapper() {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Event event = new Event();
			
			event.setId(       		rs.getInt("id"));
			event.setStudentid(		rs.getString("studentid"));
			event.setTimeline( 		rs.getInt("timeline") );
			event.setTheme(			rs.getInt("theme"));
			event.setPriority(		rs.getInt("priority"));
			event.setDay(			rs.getInt("day"));
			event.setRole(			rs.getString("role"));
			event.setSite(			rs.getString("site"));
			event.setName(			rs.getString("name"));
			event.setStartdate(		rs.getString("startdate"));
			event.setEnddate(		rs.getString("enddate"));
			event.setMemo(			rs.getString("memo"));
			
			return event;
		}
	};
	
	@Override
	public List<Event> getAllEvents() {
		List<Event> events = null;
		String searchSql = String.format(SEARCH_SQL
				, "*"
				, TABLE_EVENT
				, "");
		try
		{
			events = (List<Event>)jdbcTemplate.query(searchSql, 
					new Object[]{}, 
					new RowMapperResultSetExtractor(mReportRowMap) );
		}
		catch( DataAccessException e )
		{
			e.printStackTrace();
		}
		
		return events;
	}

	@Override
	public List<Event> getEventsById(String ids) {
		return null;
	}

	@Override
	public boolean deleteEventsById(String ids) {
		return false;
	}

	@Override
	public boolean insertEvent(Event event) {
		boolean succeed = true;
		try
		{
			jdbcTemplate.update(INSERT_SQL, new Object[]{
					event.studentid,event.timeline,event.theme,event.priority,event.day,
					event.role,event.site,event.name,event.startdate,event.enddate,event.memo
			});
		}
		catch( DataAccessException e )
		{
			succeed = false;
			e.printStackTrace();
		}
		return succeed;
	}

	@Override
	public boolean updateEvent(Event e) {
		return false;
	}

	@Override
	public boolean saveAllEvents(List<Event> l) {
		boolean succeed = true; 
		// 删除原先的
		succeed = deleteAllEventsByStudentid(l.get(0).studentid);
		
		if ( succeed ){
			for(Event e:l){
				succeed = insertEvent(e);
			}
		}
		
		return succeed;
	}
	
	public boolean deleteAllEventsByStudentid(String id){
		boolean succeed = true; 
		String param = "studentid = '"+id+"'";
		String sql   = String.format(DELETE_SQL, param);
		
		try{
			jdbcTemplate.execute(sql);
		}catch( DataAccessException e ){
			succeed = false; 
		}
		
		return succeed;
	}

}
